package Offer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 滑动窗口的最大值
 *
 * @author 是阿秋啊
 * @date 2022/08/16 20:08
 **/
public class Num59I {
    public int[] maxSlidingWindow(int[] nums, int k) {
        List<Integer> list = new ArrayList<>();
        int i = 0;
        int j = k - 1;
        int maxIndex = findeIndex(nums,i,j);
        list.add(nums[maxIndex]);
        i++;
        j++;
        int len = nums.length;
        while(j < len) {
            if(maxIndex < i) {
                maxIndex = findeIndex(nums,i,j);
            }else{
                if(nums[maxIndex] <= nums[j]){
                    maxIndex = j;
                }
            }
            list.add(nums[maxIndex]);
            i++;
            j++;
        }
        Integer[] array = list.toArray(new Integer[list.size()]);
        int[] ret = Arrays.stream(array).mapToInt(Integer::valueOf).toArray();
        return ret;
    }
    public int findeIndex(int[] nums,int i,int j){
        int maxIndex = i;
        for( ; i <= j; i++){
            if(nums[i] >= nums[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}